/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.shooter;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

/**
 *
 * @author Unni Vemanchery Mana
 */
public class Spikey {
    
    private TextureRegion[] spikeyFrames;
    private TextureRegion   currentFrame;
    private Animation spikeyAnimation;
    private float stateTime;
    private boolean showSpikey = false;
    private Vector2 position   = new Vector2(100,300);
    Rectangle bounds           = new Rectangle();
    private float speed = 0.25f;
   public Spikey(){
    spikeyFrames = new TextureRegion[7];
    for(int i = 0; i<spikeyFrames.length; i++){
       spikeyFrames[i] = new TextureRegion(new Texture(Gdx.files.internal("spikey000"+i+".png")));
    }
    spikeyAnimation = new Animation(0.1f, spikeyFrames);
    stateTime = 0f;
   }
  
   public void drawSpikey(SpriteBatch batch, float playerX, float playerY){
      if(!showSpikey) return;
      stateTime += Gdx.graphics.getRawDeltaTime();
      currentFrame = spikeyAnimation.getKeyFrame(stateTime, true);
      chase(playerX, playerY);
      batch.draw(currentFrame, position.x, position.y);
      position.y -= speed; 
      if(position.y < 0) position.y = 0;
      bounds.x = position.x;
      bounds.y = position.y;
   }
   
   public void showSpikey(boolean status){
      showSpikey = status;
   }
   
   private void chase(float playerX, float playerY){
     if(playerX < position.x){
        if(position.x == playerX)  position.x=playerX;
        else
          position.x-=speed;
       }    
       if(playerX>position.x) position.x+=0.25;
   }
}
